// pages/component/spreads.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        spData: {
            startX: 0,
            endX: 0,
            xyValue: 0,
            sp: 0,
            slider: false
        },
        xsp:300,  //滑出距离
        count:3, //  每次滑出多少
    },

    /**
     * 组件的方法列表
     */
    methods: {
        bindtouchstart(res) {
            this.data.spData.startX = res.touches[0].clientX
            console.log('触摸开始', this.data.spData.startX)
        },
        bindtouchmove(res) {
            let that = this;
            this.data.spData.endX = res.touches[0].clientX
            // console.log('触摸移动', this.data.spData.endX)
            if (this.data.spData.startX - this.data.spData.endX > 0) {
                if (this.data.spData.sp > this.data.xsp) return;
                this.data.spData.sp += this.data.count;
            } else {
                if (this.data.spData.slider) {
                    timeSetData();
                    function timeSetData() {
                        that.data.spData.sp -= that.data.count;
                        that.setData({
                            spData: that.data.spData
                        })
                        if (that.data.spData.sp > 0) {
                            setTimeout(() => {
                                timeSetData();
                            }, 20)
                        }
                        else{
                            that.data.spData.sp = 0;  
                        }
                    }
                }
            }
            this.setData({
                spData: this.data.spData
            })
        },
        bindtouchend(res) {
            let that = this,
                m = 20; //灵敏度
            this.data.spData.xyValue = this.data.spData.startX - this.data.spData.endX
            // console.log('触摸结束', this.data.spData.xyValue)
            console.log(this.data.spData)
            if (this.data.spData.xyValue > 0) {
                if (this.data.spData.sp < 50) {
                    this.data.spData.sp = 0;
                } else {
                    this.data.spData.slider = true;
                    timeSetData();
                    function timeSetData(){
                        that.data.spData.sp += that.data.count;
                        console.log(that.data.spData)
                        that.setData({
                            spData: that.data.spData
                        })
                        if (that.data.spData.sp<that.data.xsp){
                            setTimeout(() => {
                                timeSetData();
                            }, 5)
                        }
                    }
                }
            } else {
                this.data.spData.sp = 0;
            }
            this.setData({
                spData: this.data.spData
            })

        },
    }
})